'use strict';
angular.module('app')
.controller('choicenessController',[ '$rootScope', '$scope','ajaxRquest', '$state','$interval','$location',function($rootScope,$scope,ajaxRquest,$state,$interval,$location) {
            document.title="驿站精选";//页面标题 驿站精选
            var ipApi=document.createElement('script');
            ipApi.setAttribute('src','https://pv.sohu.com/cityjson?ie=utf-8');
            document.getElementsByTagName('head')[0].appendChild(ipApi);
            var huiToken = $scope.getUrlParam("huiToken");
            if(huiToken=='null'){
              huiToken=null
          }
          if(huiToken!==undefined && huiToken!=="" && huiToken!==null){
            $.cookie("huiToken",huiToken);
        }
        $scope.huiToken=$.cookie("huiToken");
        /*获取url*/
        $scope.url =$location.search();
        $scope.source=$scope.url['source'];
        //console.log($scope.source);
        $scope.paging={
            'currentPage':1,
            'total':20,
            'pageSige':5
        }
        $scope.paging.currentPage=1;
        $scope.$watch('paging.currentPage',function(newValue){
            $scope.fresh=false;
            if(newValue==0){
                $scope.paging.currentPage=1;
            }
            if(newValue==1){
                $scope.fresh=true;
            }
            $interval.cancel($scope.timer)
            $scope.timer=$interval(function(){$scope.fetchData();},200)
        })
        $scope.showNodata=true
        $scope.dataList=[];
        
        $scope.makeDataList=function(){
            var obj;
            for(var i=0;i<$scope.records.length;i++){
                obj={}
                obj.id=$scope.records[i].id;
                obj.messageTitle=$scope.records[i].messageTitle;
                obj.messageIcon=$scope.records[i].messageIcon;
                obj.channelName=$scope.records[i].channelName;//渠道名称
                obj.readCount=$scope.records[i].readCount;
                obj.messageTime=$scope.records[i].messageTime;
                obj.requestUrl=$scope.records[i].requestUrl;
                obj.type=$scope.records[i].type;
                obj.advertCon=$scope.records[i].advertCon;
                obj.pageStyle=$scope.records[i].pageStyle;
                obj.adSlotId=$scope.records[i].adSlotId;
                $scope.dataList.push(obj);
             }
              if($scope.fresh){
                 var datLen=$scope.dataList.length-$scope.records.length;
                 $scope.dataList.splice(0,datLen)
              }

            }       
            //接口获取数据
            $scope.fetchData=function(){
                $scope.records=[];
                // if($scope.huiToken!=""&&$scope.huiToken!=null&&$scope.huiToken!=undefined&&$scope.huiToken!='null'){
                    // 查询我的活动列表接口– 魏鹏
                    ajaxRquest.getData("/messageInquire/hyzFeaturedList",{
                        'mobile':$scope.mobile,
                        'current':$scope.paging.currentPage+'',      //页码  是
                        'pageSize':$scope.paging.pageSige+'',
                        'unique_identify':'0a7aa75a-b15a-43a6-9edf-a46b18280b87',        //每页条数  否6a78aa8a-700a-4470-9b3f-37d11a578ddf
                        'source':$scope.source,
                        // 'userIp':returnCitySN["cip"]
                    }).then(function(res){
                        if(res.resCode=='00000'){
                            $scope.$apply(function(){
                                $scope.records=res.records;
                                console.log($scope.records)
                                if(res.records.length==0){
                                    $scope.showNodata=false
                                }
                                $scope.paging.total=res.total;
                                $scope.makeDataList();
                                $interval.cancel($scope.timer)
                            })   
                        }else{
                            if(res.resCode=='00003') {
                                $.cookie("huiToken",null);
                                $scope.formError({
                                    text:res.resMsg, //内容
                                    resCode:res.resCode
                                })
                            }
                        }
                    })
                }
           /* //点击广告的当前时间
            $scope.getNowDate=function () {  
                var date = new Date();    
                var month = date.getMonth() + 1;  
                var strDate = date.getDate();  
                var hour = date.getHours();
                var minute = date.getMinutes();
                var second = date.getSeconds();
                if (month >= 1 && month <= 9) {  
                    month = "0" + month;  
                }  
                if (strDate >= 0 && strDate <= 9) {  
                    strDate = "0" + strDate;  
                }
                if (hour >= 1 && hour <= 9) {  
                    hour = "0" + hour;  
                }
                if (minute >= 1 && minute <= 9) {  
                    minute = "0" + minute;  
                }
                if (second >= 1 && second <= 9) {  
                    second = "0" + second;  
                }
                
                var currentdate = date.getFullYear() + "-" + month + "-" + strDate+" "+hour+":"+minute+":"+second;  
                return currentdate; 
                
            }*/
            
            /*阅读量自增*/
            $scope.readCount=function(url,id,type,adSlotId){
                    // 阅读量自增接口– 郭栋
                    ajaxRquest.getData("/messageSet/updateCarefulMessage",{
                        id:id
                    }).then(function(res){
                        if(res.resCode=='00000'){
                           /*慧驿站广告签名扣费*/
                           if(type=="2"){
                            ajaxRquest.getData("/messageInquire/hyzAdertClick",{
                                adId:id,
                                adslotId:adSlotId,
                                unique_identify:"0a7aa75a-b15a-43a6-9edf-a46b18280b87",
                                sourceType:'choice',
                                userIp:returnCitySN["cip"]
                            }).then(function(res){
                                              //  $scope.unique_identify="6a78aa8a-700a-4470-9b3f-37d11a578ddf"; 
                                              // $scope.key="miibvqibadanb7AG8A9gZMQbaqefaascatYM77eXa7eaaAeaKCSy6pYO0h3NzebEpTOtKmzhQHdo/RrgTqTqxURtBUBnizpzi47AjYPTfH3rjIgBpSXW3LIGU5DnSWiGYZPuCMidaqabaAbst2iB4pdxdiquIQWsMk69O0Xjkf+GxUwn2ZPpyDbgfiCJZB7WmIX15LCwHcvXb+8Hy6rHusUr7XPuxayzkpANa9eaUj4PrJtCQOXpW4H9aXFdH+4u7Y8xeKL2NLiKRnveFlmciqdu6ZQbCFMZZKAADqmN0hBBPmUUO0236m2oyyCk5lYu5qi8aiU1buMzNpFRI7nHbhEltCNHRWosdnx8+1HeXtcENY6va9eaQfYNueBtLeZj/Kuyjjy9Rh7Eh/6Gi/IwRTAZ24njsYecibvF6u9BIy139QfazOY/k9PyOh0WQV3DXKHonQ6PdaQf";
                                              // $scope.str = "unique_identify="+unique_identify+"&requestTime="+$scope.getNowDate()+"&key="+$scope.key;  
                                              // $scope.sign= MD5.md5($scope.str);
                                              // cosole.log($scope.sign);
                                          })
                            
                        }
                        window.location.href=url
                    }else{
                     $scope.formError({
                                 text:res.resMsg, //内容
                                 resCode:res.resCode
                             })
                 }
             })               
            }
         } ]);
